package swing;

import app.Main;
import cat.Cat;
import packages.db.DBUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static app.Main.viewChange;

public class ListView extends JPanel{
    public JPanel JPanel0;
    private JPanel JPanel1;
    private JPanel JPanel2;
    private JTable catTable;
    private JScrollPane JScrollPane;
    private JLabel TitleLabel;
    private JPanel JPanel3;
    private JButton Button;



    ArrayList<Cat> catsAarry = new ArrayList<>();
    String[] columnName = {"昵称","种类","颜色","经常出没位置","性格"};
    String[][] tableData = null;

    //创建一个表格模型
    DefaultTableModel model = null;

    public ListView() throws IOException {
        Font font=new Font("楷体", Font.BOLD,30);
        TitleLabel.setFont(font);

        font = new Font("楷体", Font.BOLD,16);
        Button.setFont(font);

        Color color = new Color(0xff, 0xff, 0xff);
        Button.setBackground(color);


        JPanel0.setOpaque(false);
        JPanel1.setOpaque(false);
        JPanel2.setOpaque(false);
        JPanel3.setOpaque(false);
        JScrollPane.setOpaque(false);
        catTable.setOpaque(false);

        //将JTable组件加入JScrollPane组件中
        JScrollPane.setViewportView(catTable);

        Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewChange = 3;
                synchronized (Main.object){
                    Main.object.notify();
                }
            }
        });
    }


    public void showCat(){
        // 清空之前的数组
        catsAarry.clear();

        // 查数据库，获取所有的cat数组
        DBUtil db = new DBUtil();
        // select
        Object[] objs = {};
        ResultSet set = db.select("select * from viewcatinfo", objs);
        try {
            while (set.next()) {
                Cat cat = new Cat(
                        set.getInt("cat_id"),
                        set.getString("cat_name"),
                        set.getString("category"),
                        set.getString("color"),
                        set.getString("location"),
                        set.getString("personality")
                );
                catsAarry.add(cat);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        db.closeConnection();

        // 由cat数组得到表结构
        String[][] catsData = new String[catsAarry.size()][];

        for(int i = 0; i < catsAarry.size(); i++){
            catsData[i] = catsAarry.get(i).getCatInArray();
        }

        tableData = catsData;

        //创建一个表格模型
        model = new DefaultTableModel(tableData, columnName){
            @Override
            public boolean isCellEditable(int row, int col){
                return false;
            }
        };

        //JTable从表格模型那里获取数据
        catTable.setModel(model);
    }
}
